package com.company.client;

import com.company.common.FileMetaInfo;
import com.company.common.Message;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.time.LocalDateTime;

public class FileService {

    public static void copyFromLocal(String fromPath, String toPath, String receiver, String sender) {
        try {
            Socket socket = ConnSvrThdManage.get(sender).getSocket();
            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(fromPath));
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            Message<FileMetaInfo> message = new Message<>(new FileMetaInfo(bytes, fromPath, toPath), Message.MessageType.FILE_MSG, receiver, sender, LocalDateTime.now());
            new ObjectOutputStream(socket.getOutputStream()).writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(sender + "给" + receiver + "发送文件:" + fromPath + "到对方电脑目录：" + toPath);
    }

}
